package toolx

import (
	"gitee.com/workits/pkgs/configx"
	"gitee.com/workits/pkgs/utilx"
)

// NewComponent 添加组件
func NewComponent(projDir, compName string) {
	// 错误处理
	defer func() {
		if err := recover(); err != nil {
			if err == ErrExist {
				panic("🔔组件已安装，无需重新安装")
			}

			// 回退
			switch compName {
			case "http":
				_ = utilx.FsRemove(projDir+PS+"component"+PS+"server_component.go", true)
				_ = utilx.FsRemove(projDir+PS+"config"+PS+"captcha.yaml", true)
				_ = utilx.FsRemove(projDir+PS+"config"+PS+"casbin.yaml", true)
				_ = utilx.FsRemove(projDir+PS+"config"+PS+"jwt.yaml", true)
				_ = utilx.FsRemove(projDir+PS+"config"+PS+"server.yaml", true)
				_ = utilx.FsRemove(projDir+PS+"internal"+PS+"component"+PS+"router", true)
				_ = utilx.FsRemove(projDir+PS+"internal"+PS+"config"+PS+"captcha_config.go", true)
				_ = utilx.FsRemove(projDir+PS+"internal"+PS+"config"+PS+"casbin_config.go", true)
				_ = utilx.FsRemove(projDir+PS+"internal"+PS+"config"+PS+"jwt_config.go", true)
				_ = utilx.FsRemove(projDir+PS+"internal"+PS+"config"+PS+"server_config.go", true)
			case "rpc":
			case "schedule":
			}
			RollbackGlobalConfig(projDir, compName) // 回滚global.go
			RollbackGomod(projDir, compName)        // 回滚go.mod

			panic(err)
		}
	}()

	// 读取配置文件
	var wkcfg WkCfg
	if _, err := configx.UnmarshalFiles(projDir+PS+".workits", &wkcfg); err != nil {
		panic("🙅配置文件读取失败")
	}

	tplDir := projDir + PS + ".workits" + PS + "templates"
	switch compName {
	case "http":
		// 生成文件
		// ---- 修改global.go(首先执行，判断http模块是否存在)
		ModGlobalConfig(projDir, compName)
		// ---- 创建文件夹
		if err := utilx.CreateMutiDir(projDir + PS + "docs"); err != nil {
			panic("🙅目录docs创建失败")
		}
		if err := utilx.CreateMutiDir(projDir + PS + "internal" + PS + "component" + PS + "router"); err != nil {
			panic("🙅目录internal/component/router创建失败")
		}
		// ---- 生成文件
		if err := utilx.CopyFile(tplDir+PS+"docs_docs.go.tpl", projDir+PS+"docs"+PS+"docs.go", false); err != nil {
			panic("🙅文件docs/docs.go生成失败")
		}
		if err := utilx.CopyFile(tplDir+PS+"config_captcha.yaml.tpl", projDir+PS+"config"+PS+"captcha.yaml", false); err != nil {
			panic("🙅文件config/captcha.yaml生成失败")
		}
		if err := utilx.CopyFile(tplDir+PS+"config_casbin.yaml.tpl", projDir+PS+"config"+PS+"casbin.yaml", false); err != nil {
			panic("🙅文件config/casbin.yaml生成失败")
		}
		if err := utilx.CopyFile(tplDir+PS+"config_jwt.yaml.tpl", projDir+PS+"config"+PS+"jwt.yaml", false); err != nil {
			panic("🙅文件config/jwt.yaml生成失败")
		}
		if err := utilx.CopyFile(tplDir+PS+"config_server.yaml.tpl", projDir+PS+"config"+PS+"server.yaml", false); err != nil {
			panic("🙅文件config/server.yaml生成失败")
		}
		if err := utilx.CopyFile(tplDir+PS+"internal_config_captcha_config.go.tpl", projDir+PS+"internal"+PS+"config"+PS+"captcha_config.go", false); err != nil {
			panic("🙅文件internal/config/captcha_config.go生成失败")
		}
		if err := utilx.CopyFile(tplDir+PS+"internal_config_casbin_config.go.tpl", projDir+PS+"internal"+PS+"config"+PS+"casbin_config.go", false); err != nil {
			panic("🙅文件internal/config/casbin_config.go生成失败")
		}
		if err := utilx.CopyFile(tplDir+PS+"internal_config_jwt_config.go.tpl", projDir+PS+"internal"+PS+"config"+PS+"jwt_config.go", false); err != nil {
			panic("🙅文件internal/config/jwt_config.go生成失败")
		}
		if err := utilx.CopyFile(tplDir+PS+"internal_config_server_config.go.tpl", projDir+PS+"internal"+PS+"config"+PS+"server_config.go", false); err != nil {
			panic("🙅文件internal/config/server_config.go生成失败")
		}
		if err := utilx.CopyFile(tplDir+PS+"router.go.tpl", projDir+PS+"internal"+PS+"component"+PS+"router"+PS+"router.go", false); err != nil {
			panic("🙅文件internal/app/component/router/router.go生成失败")
		}
		// component/server_component.go
		ParseTemplate(tplDir+PS+"component_server_component.go.tpl", projDir+PS+"component"+PS+"server_component.go", wkcfg)
		// 添加依赖
		AppendFile(projDir+PS+"go.mod", "\nrequire github.com/gin-gonic/gin v1.9.1")
		AppendFile(projDir+PS+"go.mod", "\nrequire github.com/swaggo/swag v1.16.1")

		return
	case "rpc":
		return
	case "schedule":
		return
	}

	panic("🔔组件名称不正确, 暂时仅支持: http, schedule")
}
